//
//  LLAccountViewController.m
//  Electric Scale
//
//  Created by 郭孝通 on 2018/12/11.
//  Copyright © 2018 郭孝通. All rights reserved.
//

#import "AccountViewController.h"
#import "AppDelegate.h"
#import "RegisterViewController.h"
#import "ForgetPasswordViewController.h"
#import "CustomTextField.h"
#import "CustomTabbar.h"

typedef NS_ENUM(NSInteger, AccountType) {
    AccountNormal = 0,
    AccountAdmin,
};

@interface AccountViewController ()
@property (weak, nonatomic) IBOutlet UIButton *returnButton;
@property (weak, nonatomic) IBOutlet UILabel *otherEnter;
@property (weak, nonatomic) IBOutlet UIButton *weixinEnter;
@property (weak, nonatomic) IBOutlet UIButton *qqEnter;
@property (weak, nonatomic) IBOutlet UIButton *weiboEnter;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *mima;
@property (weak, nonatomic) IBOutlet UIButton *yanzhengma;
@property (weak, nonatomic) IBOutlet UILabel *line;

@property (weak, nonatomic) IBOutlet UIButton *huoquyanzhengmaButton;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;
@property (weak, nonatomic) IBOutlet CustomTextField *phoneNumberText;
@property (weak, nonatomic) IBOutlet CustomTextField *passwordText;

- (IBAction)login:(id)sender;
- (IBAction)mimaEnter:(id)sender;
- (IBAction)yanzhengmaEnter:(id)sender;
- (IBAction)return:(id)sender;
- (IBAction)register:(id)sender;
- (IBAction)Enter_weixin:(id)sender;
- (IBAction)Enter_qq:(id)sender;
- (IBAction)Enter_weibo:(id)sender;
- (IBAction)forgetPassword:(id)sender;


@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _mima.selected = YES;
    _yanzhengma.selected = NO;
    _passwordText.placeholder = @"请输入密码，至少6位";
    _forgetButton.hidden = NO;
    _forgetButton.enabled = YES;
    _huoquyanzhengmaButton.hidden = YES;
    _huoquyanzhengmaButton.enabled = NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)return:(id)sender {
    AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CustomTabBar *tabbar = [[_rootDelegate.tabBarController.tabBar subviews] lastObject];
    [tabbar setSelectedIndex:1];
    [self dismissViewControllerAnimated:YES
                             completion:nil];
    
}

- (IBAction)register:(id)sender {
    RegisterViewController *registerViewController = [[RegisterViewController alloc] init];
    [self presentViewController:registerViewController
                       animated:YES
                     completion:nil];
    
}



- (IBAction)mimaEnter:(id)sender {
    _mima.selected = YES;
    _yanzhengma.selected = NO;
    _passwordText.placeholder = @"请输入密码，至少6位";
    _forgetButton.hidden = NO;
    _forgetButton.enabled = YES;
    _huoquyanzhengmaButton.hidden = YES;
    _huoquyanzhengmaButton.enabled = NO;
    
}

- (IBAction)yanzhengmaEnter:(id)sender {
    _yanzhengma.selected = YES;
    _mima.selected = NO;
    _passwordText.placeholder = @"请输入短信验证码";
    _forgetButton.hidden = YES;
    _forgetButton.enabled = NO;
    _huoquyanzhengmaButton.hidden = NO;
    _huoquyanzhengmaButton.enabled = YES;
}

- (IBAction)Enter_weixin:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"微信开发者账号审核中"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"返回"
                                                     style:UIAlertActionStyleDefault
                                                   handler:nil];
    [alert addAction:action];
    [self presentViewController:alert
                       animated:YES
                     completion:nil];
    
}

- (IBAction)Enter_qq:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"QQ开发者账号审核中"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"返回"
                                                     style:UIAlertActionStyleDefault
                                                   handler:nil];
    [alert addAction:action];
    [self presentViewController:alert
                       animated:YES
                     completion:nil];
}

- (IBAction)Enter_weibo:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"微博开发者账号审核中"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"返回"
                                                     style:UIAlertActionStyleDefault
                                                   handler:nil];
    [alert addAction:action];
    [self presentViewController:alert
                       animated:YES
                     completion:nil];
}

- (IBAction)forgetPassword:(id)sender {
    ForgetPasswordViewController *vt = [[ForgetPasswordViewController alloc] init];
    [self presentViewController:vt
                       animated:YES
                     completion:nil];
}






- (IBAction)login:(id)sender {
    NSString* accont = self.phoneNumberText.text;
    NSString* password = self.passwordText.text;
    
    if(self.mima.selected){
        if([accont isEqualToString:@""] || [password isEqualToString:@""] || password.length < 6){
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"输入错误！" message:@"请输入正确的账号/密码格式" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }
        else{
            if([accont isEqualToString:@"admin"] && [password isEqualToString:@"123456"]){
                AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
                appDelegate.userModel.userName = accont;
                appDelegate.userModel.userType = @"管理员";
                appDelegate.userModel.cookie = [NSData data];
                [self dismissViewControllerAnimated:YES completion:nil];
                return;
            }
            if([accont isEqualToString:@"user"] && [password isEqualToString:@"123456"]){
                AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
                appDelegate.userModel.userName = accont;
                appDelegate.userModel.userType = @"用户";
                appDelegate.userModel.cookie = [NSData data];
                [self dismissViewControllerAnimated:YES completion:nil];
                return;
            }

            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"账号或密码错误！" message:@"请联系客服或找回密码" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    else{
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"短信验证码错误！" message:@"请检查手机是否正确，并重发验证码" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
//    if(_passwordText.text != nil){
//        [self dismissViewControllerAnimated:self
//                                 completion:nil];
//        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
//        if(![_rootDelegate.userModel.userType isEqualToString:@"管理员"]){
//            _rootDelegate.userModel.userType = @"用户";
//        }
//        _rootDelegate.userModel.cookie = [NSData data];
//        _rootDelegate.userModel.userName = [NSString stringWithFormat:@"catalpa_e"];
//    }
}




@end



